package com.sap.apitest2.deprecated.process;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.sap.apitest2.entity.AbstractEntity;



public class EntityProcess {
	
	
	public void saveEntity(AbstractEntity entity)throws Exception{
		EntityManager em = this.getEntityManager();
		try{	
			em.getTransaction().begin();
			if(entity.getKey()!=null && entity.getKey()>0){
				
				AbstractEntity target = em.find(entity.getClass(), entity.getKey());
				if(target!=null){
					target.acceptValue(entity);
					em.merge(target);
				}
				
			}
			else{
				em.persist(entity);		
			}
			em.getTransaction().commit();	
			
			System.out.println("entity "+entity.getKey());
		}
		catch(Exception ex){
			em.getTransaction().rollback();
			throw ex;
		}
		finally{
			this.releaseEntityManager(em);
		}
//		EntityManager em = this.getEntityManager();
//		try{	
//			
//			
//			em.persist(entity);		
//		}
//		catch(Exception ex){
//			//this.LogException(ex);
//			throw ex;
//		}
//		finally{
//			this.releaseEntityManager(em);
//		}
	}
	
	public <T extends AbstractEntity> void saveEntities(Long parentId, Collection<T> entities, Collection<T> exists)throws Exception{
		
		Map<Long, T> compare = new HashMap<Long, T>();
		
		if(entities!=null){
			for(T entity : entities){
				compare.put(entity.getKey(), entity);
			}
		}
		if(exists!=null){
			for(T step : exists){
				if(compare.get(step.getKey())==null){
					this.removeEntity(step);
				}
			}
		}
		if(entities!=null){
			for(T entity : entities){
				this.saveEntity(entity);
			}
		}
	}
	
	
	public void removeEntity(AbstractEntity entity)throws Exception{
		EntityManager em = this.getEntityManager();
		try{
			//entity=em.merge(entity);
			AbstractEntity result= em.find(entity.getClass(), entity.getKey());	
			
			em.remove(result);		
		}
		catch(Exception ex){
			//ex.printStackTrace();
			throw ex;
		}
		finally{
			this.releaseEntityManager(em);
		}
	}	
	public <T extends AbstractEntity> List<T> getEntities(String name)throws Exception{
		EntityManager em = this.getEntityManager();
		try{			
			Query q = em.createQuery("SELECT p FROM "+name+" p ");
			//q.setParameter(1, id);
			
		    @SuppressWarnings("unchecked")
			List<T> results = q.getResultList();			
			return results;
		}
		catch(Exception ex){
			throw ex;//this.LogException(ex);
		}
		finally{
			this.releaseEntityManager(em);
		}
	}
	protected EntityManager getEntityManager(){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("apitest");
		EntityManager em = emf.createEntityManager();
		return em;
	}
	
	protected void releaseEntityManager(EntityManager em){
		if(em!=null) em.close();
	}
	
	protected void LogException(Exception e){
		e.printStackTrace();
	}
	
	public <T extends AbstractEntity> T getEntity(Class<T> cls, Long id)throws Exception{
		EntityManager em = this.getEntityManager();
		try{			
			T result= (T) em.find(cls, id);	
			return result;
		}
		
		catch(Exception ex){
			throw ex;//this.LogException(ex);
		}
		finally{
			this.releaseEntityManager(em);
		}
	}
}
